package net.hn.hnms.biz.energy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.energy.domain.WaterRecord;
import net.hn.hnms.biz.energy.mapper.WaterRecordMapper;
import net.hn.hnms.biz.energy.request.WaterRecordReq;
import net.hn.hnms.biz.energy.service.WaterRecordService;
import net.hn.hnms.biz.energy.vo.WaterRecordVo;
import net.hn.hnms.biz.energy.vo.WaterTotalVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WaterRecordServiceImpl implements WaterRecordService {
    @Autowired
    private WaterRecordMapper waterRecordMapper;

    @Override
    public int insert(WaterRecordReq waterRecordReq) {
        WaterRecord insertObj = new WaterRecord();
        BeanUtil.copyProperties(waterRecordReq, insertObj);
        return waterRecordMapper.insert(insertObj);
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        waterRecordMapper.updateDeleteByIds(idList);
    }

    @Override
    public void updateById(WaterRecordReq waterRecordReq) {
        WaterRecord updateObj = new WaterRecord();
        BeanUtil.copyProperties(waterRecordReq, updateObj);
        int updateCount = waterRecordMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public WaterRecord selectDetailById(String id) {
        WaterRecord waterRecord = waterRecordMapper.selectById(id);
        if (waterRecord == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return waterRecord;
    }

    @Override
    public ResponsePage<WaterRecordVo> selectPageList(WaterRecordReq waterRecordReq, PageQuery pageQuery) {
//        Page<WaterRecord> page = waterRecordMapper.selectPage(pageQuery.build(), buildQuery(waterRecordReq).orderByDesc(WaterRecord::getCreateTime));
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
        Page<WaterRecordVo> page = waterRecordMapper.selectPageList(pageQuery.build(), waterRecordReq);
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    @Override
    public WaterTotalVo getTotal(WaterRecordReq waterRecordReq) {
        return waterRecordMapper.getTotal(waterRecordReq);
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<WaterRecord> buildQuery(WaterRecordReq req){
        return new LambdaQueryWrapper<WaterRecord>()
                .eq(StrUtil.isNotBlank(req.getWaterSupplyCompanyId()), WaterRecord::getWaterSupplyCompanyId, req.getWaterSupplyCompanyId())
                .ge(req.getBeginDateTime() != null, WaterRecord::getCreateTime, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, WaterRecord::getCreateTime, req.getEndDateTime());
    }
}
